package com.shoujifeng.snowmusic.player;

import java.util.ArrayList;
import java.util.List;

import com.shoujifeng.snowmusic.player.LrcProcess.LrcContent;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;


/**
 * 閼奉亜鐣炬稊澶岀帛閻㈢粯鐡曠拠宥忕礉娴溠呮晸濠婃艾濮╅弫鍫熺亯
 */
public class LrcView extends TextView {

	private float width;
	private float high;
	private Paint CurrentPaint;
	private Paint NotCurrentPaint;
	private float TextHigh = 35;
	private float TextSize = 30;
	private int Index = 0;

	List<LrcContent> mSentenceEntities = new ArrayList<LrcContent>();

	 public void setmLrcList(List<LrcContent> mLrcList) {  
		         this.mSentenceEntities = mLrcList;  
		     }
	 public void setIndex(int index) {  
		         this.Index = index;  
		    } 

	public LrcView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	public LrcView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init();
	}

	public LrcView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		setFocusable(true);

		// 妤傛ü瀵掗柈銊ュ瀻
		CurrentPaint = new Paint();
		CurrentPaint.setAntiAlias(true);
		CurrentPaint.setTextAlign(Paint.Align.CENTER);

		// 闂堢偤鐝禍顕�劥閸掞拷
		NotCurrentPaint = new Paint();
		NotCurrentPaint.setAntiAlias(true);
		NotCurrentPaint.setTextAlign(Paint.Align.CENTER);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		if (canvas == null) {
			return;
		}

		CurrentPaint.setColor(Color.argb(255, 255, 255, 255));
		NotCurrentPaint.setColor(Color.argb(255, 197, 197, 197));

		CurrentPaint.setTextSize(30);
		CurrentPaint.setTypeface(Typeface.SERIF);

		NotCurrentPaint.setTextSize(TextSize);
		NotCurrentPaint.setTypeface(Typeface.DEFAULT);

		try {
			setText("");
			if(mSentenceEntities.size() <= 0)
			{
				setText("没有找到歌词!");
				return;
			}
			
			canvas.drawText(mSentenceEntities.get(Index).getLrc(), width / 2,
					high / 2, CurrentPaint);

			float tempY = high / 2;
			// 閻㈣鍤張顒�綖娑斿澧犻惃鍕綖鐎涳拷
			for (int i = Index - 1; i >= 0; i--) {
				// 閸氭垳绗傞幒銊�
				tempY = tempY - TextHigh;

				canvas.drawText(mSentenceEntities.get(i).getLrc(), width / 2,
						tempY, NotCurrentPaint);
			}
			tempY = high / 2;
			// 閻㈣鍤張顒�綖娑斿鎮楅惃鍕綖鐎涳拷
			for (int i = Index + 1; i < mSentenceEntities.size(); i++) {
				// 瀵帮拷绗呴幒銊�
				tempY = tempY + TextHigh;
				canvas.drawText(mSentenceEntities.get(i).getLrc(), width / 2,
						tempY, NotCurrentPaint);
			}
		} catch (Exception e) {
			e.printStackTrace();
			setText("没有找到歌词!");
		}
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		super.onSizeChanged(w, h, oldw, oldh);

		this.width = w;
		this.high = h;
	}

	public void SetIndex(int index) {
		this.Index = index;
	}
}
